#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:zhangcong
# Email:zc_92@sina.com


from django import template
from django.utils.safestring import mark_safe
from web import models
register = template.Library()


# 输出跟进记录
@register.simple_tag
def print_follow_records(follow_records_obj):

    result_html = ""
    for index, obj in enumerate(follow_records_obj, start=1):
        if index % 2 == 1:  # 单数
            temp_html = """
                <li class="timeline-item">
                    <div class="timeline-dot" data-placement="left" data-toggle="tooltip" data-trigger="hover"></div>
                    <div class="timeline-content">
                        <div class="widget widget-article widget-shadow">
                            {image_html}
                            <div class="widget-body padding-20">
                                <h3>{time}</h3>
                                <p>{content}</p>
                            </div>
                        </div>
                    </div>
                </li>
            """
        else:
            temp_html = """
                <li class="timeline-item timeline-reverse">
                    <div class="timeline-dot" data-placement="left" data-toggle="tooltip" data-trigger="hover"></div>
                    <div class="timeline-content">
                        <div class="widget widget-article widget-shadow">
                            {image_html}
                            <div class="widget-body padding-20">
                                <h3>{time}</h3>
                                <p>{content}</p>
                            </div>
                        </div>
                    </div>
                </li>
            """
        if obj.image_url:
            image_html = """
                <div class="widget-header cover">
                    <img class="cover-image" src="{image_url}">
                </div>
            """.format(image_url=obj.image_url)
        else:
            image_html = ""

        result_html += temp_html.format(image_html=image_html, time=obj.update_date.strftime('%Y-%m-%d %H:%M:%S'), content=obj.content)

    return mark_safe(result_html)


# 输出更近记录截图html
@register.simple_tag
def print_img_html(image_url):
    image_html = ""
    if image_url:
        for index, i in enumerate(image_url.strip('|').split('|'), start=1):
            image_html += '<a href="{url}" target = "_blank" > 截图{index} </a>'.format(url=i, index=index)

    return mark_safe(image_html)



